package com.stoneframe.strokeserver.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.stoneframe.base.mongodb.BeanUtil;
import com.stoneframe.base.util.DateHelper;
import com.stoneframe.base.util.PageData;
import com.stoneframe.base.util.Pagination;
import com.stoneframe.base.util.ToolHelper;
import com.stoneframe.strokeserver.model.TrackModel;

@Service(value="trackService")
public class TrackService {
	
	@Resource(name="mongoTemplate")
	private MongoTemplate mongoTemplate;
	
	public List<TrackModel> listAll(PageData pd) throws Exception{
		List<TrackModel> list=new ArrayList();
		DBObject query=new BasicDBObject();
		
		if(pd.containsKey("uid")){
			query.put("uid", pd.getString("uid"));
		}
		DBObject sort=new BasicDBObject();
		// 1,表示正序； －1,表示倒序
		sort.put("track_time", -1);
		
		DBCursor result=mongoTemplate.getCollection("track_info").find(query).sort(sort);
		if(null!=result){
			Iterator<DBObject> iterator=result.iterator();
			while(iterator.hasNext()){
				BasicDBObject obj=(BasicDBObject)iterator.next();
				TrackModel trackModel=new TrackModel();
				trackModel=BeanUtil.dbObject2Bean(obj, trackModel);
				list.add(trackModel);
			}
		}
		return list;
	}
	
	public List<PageData> listPage(Pagination page) throws Exception{
		List<PageData> list=new ArrayList();
		DBObject query=new BasicDBObject();
		if(page.getPd().containsKey("uid")){
			query.put("uid", page.getPd().get("uid"));
		}
		DBObject sort=new BasicDBObject();
		if(ToolHelper.notEmpty(page.getSidx())&&ToolHelper.notEmpty(page.getSord())){
			sort.put(page.getSidx(), page.getSord());
		}else{
			// 1,表示正序； －1,表示倒序
			sort.put("track_time", -1);
		}
		DBCursor cur=mongoTemplate.getCollection("track_info").find(query).sort(sort);
		page.setRecords(cur.count());
		cur=cur.skip(page.getPage()).limit(page.getRows());
		while(cur.hasNext()){
			DBObject obj=cur.next();
			PageData pd=new PageData();
			pd.put("trackId",obj.get("trackId"));
			pd.put("terinalPN",obj.get("terinalPN"));
			pd.put("trackTime",DateHelper.parseDate((Long)obj.get("trackTime")));
			pd.put("trackLat",obj.get("trackLat"));
			pd.put("trackLon",obj.get("trackLon"));
			list.add(pd);
		}
		return list;
		
	}
	
	public int save(PageData pd) throws Exception{
		DBObject trackObj=new BasicDBObject();
		trackObj.put("trackId",pd.getString("trackId"));
		trackObj.put("terinalPN",pd.getString("terinalPN"));
		trackObj.put("trackTime",new Date(pd.getString("trackTime")).getTime());
		trackObj.put("trackLat",pd.get("trackLat"));
		trackObj.put("trackLon",pd.get("trackLon"));
		
		WriteResult writeResult=mongoTemplate.getCollection("track_info").save(trackObj);
		return writeResult.getN();
	}
	
	public int edit(PageData pd) throws Exception{
		DBObject trackObj=new BasicDBObject();
		DBObject queryObj=new BasicDBObject();
		trackObj.put("trackId",pd.getString("trackId"));
		trackObj.put("terinalPN",pd.getString("terinalPN"));
		trackObj.put("trackTime",new Date(pd.getString("trackTime")).getTime());
		trackObj.put("trackLat",pd.get("trackLat"));
		trackObj.put("trackLon",pd.get("trackLon"));
		queryObj.put("trackId",pd.getString("trackId"));
		WriteResult result= mongoTemplate.getCollection("track_info").update(queryObj, trackObj);
		return result.getN();
	}
	
	public int del(PageData pd) throws Exception{
		DBObject query=new BasicDBObject();
		if(pd.containsKey("uid")){
			query.put("uid", pd.getString("uid"));
		}
		if(query.toMap().isEmpty()){
			return 0;
		}
		WriteResult result=mongoTemplate.getCollection("track_info").remove(query);
		return result.getN();
	}

}
